package com.chen106106.sstest.common.memcached;

import java.util.HashMap;
import net.spy.memcached.MemcachedClient;
import net.spy.memcached.OperationTimeoutException;
import org.apache.commons.lang.StringUtils;

import java.util.List;
import java.util.Map;

/**
 * 
 * @author zhenai
 * 2012-7-18 下午2:07:02
 */
public class MemcachedCache implements Cache {
	public MemcachedCache() {
	}

	public void set(String key, Object value, int expiry) {
		if (StringUtils.isEmpty(key) || value == null) {
			return;
		} else {
			memcachedClient.set(key, expiry * 60, value);
			return;
		}
	}

	public Object get(String key) {
		if (StringUtils.isEmpty(key))
			return Boolean.valueOf(false);
		Object o;
		try {
			o = memcachedClient.get(key);
		} catch (OperationTimeoutException e) {
			o = memcachedClient.get(key);
		}
		return o;
	}

	public Map getMulti(List keys) {
		if (keys == null || keys.size() == 0) {
			return new HashMap(0);
		} else {
			String strKeys[] = new String[keys.size()];
			strKeys = (String[]) keys.toArray(strKeys);
			return memcachedClient.getBulk(strKeys);
		}
	}

	public Object[] getMulti(String keys[]) {
		if (keys == null || keys.length == 0) {
			return new Object[0];
		} else {
			Map map = memcachedClient.getBulk(keys);
			return map.values().toArray();
		}
	}

	public void delete(String key) {
		if (StringUtils.isEmpty(key)) {
			return;
		} else {
			memcachedClient.delete(key);
			return;
		}
	}

	public boolean exists(String key) {
		if (StringUtils.isEmpty(key))
			return false;
		else
			return memcachedClient.get(key) != null;
	}

	public void setMemcachedClient(MemcachedClient memcachedClient) {
		this.memcachedClient = memcachedClient;
	}

	private MemcachedClient memcachedClient;

}
